package org.xa3apm.vote.hibernate.entities;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity(name = "Person")
@Table(name = "tPerson")
@DiscriminatorColumn(name = "Discriminator", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("Person")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@NamedQueries({
		@NamedQuery(name = "Person.getAll", query = "SELECT P FROM Person P"),
		@NamedQuery(name = "Person.getById", query = "SELECT P FROM Person P WHERE P.id = :id") })
public class Person implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = -4858829452307915622L;
	@Id
	private Long id;
	@Column(name = "birth_day", length = 10)
	@Temporal(TemporalType.DATE)
	private Date birthDay;
	@Column(name = "fName")
	private String fName;
	@Column(name = "mName")
	private String mName;
	@Column(name = "lName")
	private String lName;
	@OneToOne(mappedBy = "person")
	private Address address;
	@OneToOne(mappedBy = "person")
	private Account account;

	public Person() {
		// TODO Auto-generated constructor stub
	}

	public Person(Long id, Date birthDay, String fName, String mName,
			String lName, Address address, Account account) {
		super();
		this.id = id;
		this.birthDay = birthDay;
		this.fName = fName;
		this.mName = mName;
		this.lName = lName;
		this.address = address;
		this.account = account;
	}

	/**
	 * @return the id
	 */
	public final Long getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public final void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the birthDay
	 */
	public final Date getBirthDay() {
		return birthDay;
	}

	/**
	 * @param birthDay
	 *            the birthDay to set
	 */
	public final void setBirthDay(Date birthDay) {
		this.birthDay = birthDay;
	}

	/**
	 * @return the fName
	 */
	public final String getfName() {
		return fName;
	}

	/**
	 * @param fName
	 *            the fName to set
	 */
	public final void setfName(String fName) {
		this.fName = fName;
	}

	/**
	 * @return the mName
	 */
	public final String getmName() {
		return mName;
	}

	/**
	 * @param mName
	 *            the mName to set
	 */
	public final void setmName(String mName) {
		this.mName = mName;
	}

	/**
	 * @return the lName
	 */
	public final String getlName() {
		return lName;
	}

	/**
	 * @param lName
	 *            the lName to set
	 */
	public final void setlName(String lName) {
		this.lName = lName;
	}

	/**
	 * @return the address
	 */
	public final Address getAddress() {
		return address;
	}

	/**
	 * @param address
	 *            the address to set
	 */
	public final void setAddress(Address address) {
		this.address = address;
	}

	/**
	 * @return the account
	 */
	public final Account getAccount() {
		return account;
	}

	/**
	 * @param account
	 *            the account to set
	 */
	public final void setAccount(Account account) {
		this.account = account;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((account == null) ? 0 : account.getId().hashCode());
		result = prime * result
				+ ((address == null) ? 0 : address.getId().hashCode());
		result = prime * result
				+ ((birthDay == null) ? 0 : birthDay.hashCode());
		result = prime * result + ((fName == null) ? 0 : fName.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((lName == null) ? 0 : lName.hashCode());
		result = prime * result + ((mName == null) ? 0 : mName.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Person)) {
			return false;
		}
		Person other = (Person) obj;
		if (account == null) {
			if (other.account != null) {
				return false;
			}
		} else if (!account.getId().equals(other.account.getId())) {
			return false;
		}
		if (address == null) {
			if (other.address != null) {
				return false;
			}
		} else if (!address.getId().equals(other.address.getId())) {
			return false;
		}
		if (birthDay == null) {
			if (other.birthDay != null) {
				return false;
			}
		} else if (!birthDay.equals(other.birthDay)) {
			return false;
		}
		if (fName == null) {
			if (other.fName != null) {
				return false;
			}
		} else if (!fName.equals(other.fName)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (lName == null) {
			if (other.lName != null) {
				return false;
			}
		} else if (!lName.equals(other.lName)) {
			return false;
		}
		if (mName == null) {
			if (other.mName != null) {
				return false;
			}
		} else if (!mName.equals(other.mName)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Person [id=" + id + ", birthDay=" + birthDay + ", fName="
				+ fName + ", mName=" + mName + ", lName=" + lName
				+ ", addressId=" + address.getId() + ", accountId="
				+ account.getId() + "]";
	}

}
